home *** CD-ROM | disk | FTP | other *** search
/ Aminet 48 / Aminet 48 (2002)(GTI - Schatztruhe)[!][Apr 2002].iso / Aminet / text / edit / vim60rt.lha / Vim / vim60 / doc / usr_25.txt < prev    next >
Encoding:
Text File  |  2001-09-26  |  16.9 KB  |  527 lines

  1. *usr_25.txt*    For Vim version 6.0.  Last change: 2001 Sep 03
  2.  
  3.              VIM USER MANUAL - by Bram Moolenaar
  4.  
  5.                  Editing formatted text
  6.  
  7.  
  8. Text hardly ever comes in one sentence per line.  This chapter is about
  9. breaking sentences to make them fit on a page and other formatting.
  10. Vim also has useful features for editing single-line paragraphs and tables.
  11.  
  12. |25.1|    Breaking lines
  13. |25.2|    Aligning text
  14. |25.3|    Indents and tabs
  15. |25.4|    Dealing with long lines
  16. |25.5|    Editing tables
  17.  
  18.      Next chapter: |usr_26.txt|  Repeating
  19.  Previous chapter: |usr_24.txt|  Inserting quickly
  20. Table of contents: |usr_toc.txt|
  21.  
  22. ==============================================================================
  23. *25.1*    Breaking lines
  24.  
  25. Vim has a number of functions that make dealing with text easier.  By default,
  26. the editor does not perform automatic line breaks.  In other words, you have
  27. to press <Enter> yourself.  This is useful when you are writing programs where
  28. you want to decide where the line ends.  It is not so good when you are
  29. creating documentation and want the text to be at most 70 character wide.
  30.    If you set the 'textwidth' option, Vim automatically inserts line breaks.
  31. Suppose, for example, that you want a very narrow column of only 30
  32. characters.  You need to execute the following command: >
  33.  
  34.     :set textwidth=30
  35.  
  36. Now you start typing (ruler added):
  37.  
  38.          1       2         3
  39.     12345678901234567890123456789012345
  40.     I taught programming for a whi ~
  41.  
  42. If you type "l" next, this makes the line longer than the 30-character limit.
  43. When Vim sees this, it inserts a line break and you get the following:
  44.  
  45.          1       2         3
  46.     12345678901234567890123456789012345
  47.     I taught programming for a ~
  48.     whil ~
  49.  
  50. Continuing on, you can type in the rest of the paragraph:
  51.  
  52.          1       2         3
  53.     12345678901234567890123456789012345
  54.     I taught programming for a ~
  55.     while. One time, I was stopped ~
  56.     by the Fort Worth police, ~
  57.     because my homework was too ~
  58.     hard. True story. ~
  59.  
  60. You do not have to type newlines; Vim puts them in automatically.
  61.  
  62.     Note:
  63.     The 'wrap' option makes Vim display lines with a line break, but this
  64.     doesn't insert a line break in the file.
  65.  
  66.  
  67. REFORMATTING
  68.  
  69. The Vim editor is not a word processor.  In a word processor, if you delete
  70. something at the beginning of the paragraph, the line breaks are reworked.  In
  71. Vim they are not; so if you delete the word "programming" from the first line,
  72. all you get is a short line:
  73.  
  74.          1       2         3
  75.     12345678901234567890123456789012345
  76.     I taught for a ~
  77.     while. One time, I was stopped ~
  78.     by the Fort Worth police, ~
  79.     because my homework was too ~
  80.     hard. True story. ~
  81.  
  82. This does not look good.  To get the paragraph into shape you use the "gq"
  83. operator.
  84.    Let's first use this with a Visual selection.  Starting from the first
  85. line, type: >
  86.  
  87.     v4jgq
  88.  
  89. "v" to start Visual mode, "4j' to move to the end of the paragraph and then
  90. the "gq" operator.  The result is:
  91.  
  92.          1       2         3
  93.     12345678901234567890123456789012345
  94.     I taught for a while. One ~
  95.     time, I was stopped by the ~
  96.     Fort Worth police, because my ~
  97.     homework was too hard. True ~
  98.     story. ~
  99.  
  100. Since "gq" is an operator, you can use one of the three ways to select the
  101. text it works on: With Visual mode, with a movement and with a text object.
  102.    The example above could also be done with "gq4j".  That's less typing, but
  103. you have to know the line count.  A more useful motion command is "}".  This
  104. moves to the end of a paragraph.  Thus "gq}" formats from the cursor to the
  105. end of the current paragraph.
  106.    A very useful text object to use with "gq" is the paragraph.  Try this: >
  107.  
  108.     gqap
  109.  
  110. "ap" stands for "a-paragraph".  This formats the text of one paragraph
  111. (separated by empty lines).  Also the part before the cursor.
  112.    If you have your paragraphs separated by empty lines, you can format the
  113. whole file by typing this: >
  114.  
  115.     gggqG
  116.  
  117. "gg" to move to the first line, "gqG" to format until the last line.
  118.    Warning: If your paragraphs are not properly separated, they will be joined
  119. together.  A common mistake is to have a line with a space or Tab.  That's a
  120. blank line, but not an empty line.
  121.  
  122. Vim is able format more than just plain text.  See |fo-table| for how to
  123. change this.  See the 'joinspaces' option to change the number of spaces used
  124. after a full stop.
  125.    It is possible to use an external program for formatting.  This is useful
  126. if your text can't be properly formatted with Vim's builtin command.  See the
  127. 'formatprg' option.
  128.  
  129. ==============================================================================
  130. *25.2*    Aligning text
  131.  
  132. To center a range of lines, use the following command: >
  133.  
  134.     :{range}center [width]
  135.  
  136. {range} is the usual command-line range.  [width] is an optional line width to
  137. use for centering.  If [width] is not specified, it defaults to the value of
  138. 'textwidth'.  (If 'textwidth' is 0, the default is 80.)
  139.    For example: >
  140.  
  141.     :1,5center 40
  142.  
  143. results in the following:
  144.  
  145.        I taught for a while. One ~
  146.        time, I was stopped by the ~
  147.      Fort Worth police, because my ~
  148.       homework was too hard. True ~
  149.          story. ~
  150.  
  151.  
  152. RIGHT ALIGNMENT
  153.  
  154. Similarly, the ":right" command right-justifies the text:
  155.  
  156.     :1,5right 30
  157.  
  158. gives this result:
  159.  
  160.         I taught for a while. One ~
  161.        time, I was stopped by the ~
  162.     Fort Worth police, because my ~
  163.       homework was too hard. True ~
  164.                    story. ~
  165.  
  166. LEFT ALIGNMENT
  167.  
  168. Finally there is this command: >
  169.  
  170.     :{range}left [margin]
  171.  
  172. Unlike ":center" and ":right", however, the argument to ":left" is not the
  173. length of the line.  Instead it is the left margin.  If it is omitted, the
  174. text will be put against the left side of the screen (using a zero margin
  175. would do the same).  If it is 5, the text will be indented 5 spaces.  For
  176. example, use these commands: >
  177.  
  178.     :1left 5
  179.     :2,5left
  180.  
  181. This results in the following:
  182.  
  183.          I taught for a while. One ~
  184.     time, I was stopped by the ~
  185.     Fort Worth police, because my ~
  186.     homework was too hard. True ~
  187.     story. ~
  188.  
  189.  
  190. JUSTIFYING TEXT
  191.  
  192. Vim has no built-in way of justifying text.  However, there is a neat macro
  193. package that does the job.  To use this package, execute the following
  194. command: >
  195.  
  196.     :runtime macros/justify.vim
  197.  
  198. This Vim script file defines a new visual command "_j". To justify a block of
  199. text, highlight the text in Visual mode and then execute "_j".
  200.    Look in the file for more explanations.  To go there, do "gf" on this name:
  201. $VIMRUNTIME/macros/justify.vim.
  202.  
  203. An alternative is to filter the text through an external program.  Example: >
  204.  
  205.     :%!fmt
  206.  
  207. ==============================================================================
  208. *25.3*    Indents and tabs
  209.  
  210. Indents can be used to make text stand out from the rest.  The example texts
  211. in this manual, for example, are indented by eight spaces or a tab.  You would
  212. normally enter this by typing a tab at the start of each line.  Take this
  213. text:
  214.     the first line ~
  215.     the second line ~
  216.  
  217. This is entered by typing a tab, some text, <Enter>, tab and more text.
  218.    The 'autoindent' option inserts indents automatically: >
  219.  
  220.     :set autoindent
  221.  
  222. When a new line is started it gets the same indent as the previous line.  In
  223. the above example, the tab after the <Enter> is not needed anymore.
  224.  
  225.  
  226. INCREASING INDENT
  227.  
  228. To increase the amount of indent in a line, use the ">" operator.  Often this
  229. is used as ">>", which adds indent to the current line.
  230.    The amount of indent added is specified with the 'shiftwidth' option.  The
  231. default value is 8.  To make ">>" insert four spaces wordth of indent, for
  232. example, type this: >
  233.  
  234.     :set shiftwidth=4
  235.  
  236. When used on the second line of the example text, this is what you get:
  237.  
  238.     the first line ~
  239.         the second line ~
  240.  
  241. "4>>" will increase the indent of four lines.
  242.  
  243.  
  244. TABSTOP
  245.  
  246. If you want to make indents a multiple of 4, you set 'shiftwidth' to 4.  But
  247. when pressing a Tab you still get 8 spaces worth of indent.  To change this,
  248. set the 'softtabstop' option: >
  249.  
  250.     :set softtabstop=4
  251.  
  252. This will make the <Tab> key insert 4 spaces worth of indent.  If there are
  253. already four spaces, a <Tab> character is used (saving seven characters in the
  254. file).  (If you always want spaces and no tab characters, set the 'expandtab'
  255. option.)
  256.  
  257.     Note:
  258.     You could set the 'tabstop' option to 4.  However, if you edit the
  259.     file another time, with 'tabstop' set to the default value of 8, it
  260.     will look wrong.  In other programs and when printing the indent will
  261.     also be wrong.  Therefore it is recommended to keep 'tabstop' at eight
  262.     all the time.  That's the standard value everywhere.
  263.  
  264.  
  265. CHANGING TABS
  266.  
  267. You edit a file which was written with a tabstop of 3.  In Vim it looks ugly,
  268. because it uses the normal tabstop value of 8.  You can fix this by setting
  269. 'tabstop' to 3.  But you have to do this every time you edit this file.
  270.    Vim can change the use of tabstops in your file.  First, set 'tabstop' to
  271. make the indents look good, then use the ":retab" command: >
  272.  
  273.     :set tabstop=3
  274.     :retab 8
  275.  
  276. The ":retab" command will change 'tabstop' to 8, while changing the text such
  277. that it looks the same.  It changes spans of white space into tabs and spaces
  278. for this.  You can now write the file.  Next time you edit it the indents will
  279. be right without setting an option.
  280.    Warning: When using ":retab" on a program, it may change white space inside
  281. a string constant.  Therefore it's a good habit to use "\t" instead of a
  282. real tab
  283.  
  284. ==============================================================================
  285. *25.4*    Dealing with long lines
  286.  
  287. Sometimes you will be editing a file that is wider than the number of columns
  288. in the window.  When that occurs, Vim wraps the lines so that everything fits
  289. on the screen.
  290.    If you switch the 'wrap' option off, each line in the file shows up as one
  291. line on the screen.  Then the ends of the long lines disappear off the screen
  292. to the right.
  293.    When you move the cursor to a character that can't be seen, Vim will scroll
  294. the text to show it.  This is like moving a viewport over the text in the
  295. horizontal direction.
  296.    By default, Vim does not display a horizontal scrollbar in the GUI.  If you
  297. want to enable one, use the following command: >
  298.  
  299.     :set guioptions+=b
  300.  
  301. One horizontal scrollbar will appear at the bottom of the Vim window.
  302.  
  303. If you don't have a scrollbar or don't want to use it, use these commands to
  304. scroll the text.  The cursor will stay in the same place, but it's move back
  305. into the visible text if necessary.
  306.  
  307.     zh        scroll right
  308.     4zh        scroll four characters right
  309.     zH        scroll half a window width right
  310.     ze        scroll right to put the cursor at the end
  311.     zl        scroll left
  312.     4zl        scroll four characters left
  313.     zL        scroll half a window width left
  314.     zs        scroll left to put the cursor at the start
  315.  
  316. Let's attempt to show this with one line of text.  The cursor is on the "w" of
  317. "which".  The "current window" above the line indicates the text that is
  318. currently visible.  The "window"s below the text indicate the text that is
  319. visible after the command left of it.
  320.  
  321.                   |<-- current window -->|
  322.         some long text, part of which is visible in the window~
  323.     ze      |<--       window     -->|
  324.     zH       |<--     window     -->|
  325.     4zh          |<--       window     -->|
  326.     zh             |<--     window     -->|
  327.     zl               |<--    window       -->|
  328.     4zl              |<--       window     -->|
  329.     zL                |<--     window     -->|
  330.     zs                   |<--    window       -->|
  331.  
  332.  
  333. MOVING WITH WRAP OFF
  334.  
  335. When 'wrap' is off and the text has scrolled horizontally, you can use the
  336. following commands to move the cursor to a character you can see.  Thus text
  337. left and right of the window is ignored.  These never cause the text to
  338. scroll:
  339.  
  340.     g0        to first visible character in this line
  341.     g^        to first non-blank visible character in this line
  342.     gm        to middle of this line
  343.     g$        to last visible character in this line
  344.  
  345.         |<--     window    -->|
  346.     some long    text, part of which is visible ~
  347.          g0  g^    gm         g$
  348.  
  349.  
  350. BREAKING AT WORDS
  351.  
  352. When preparing text for use by another program, you might have to make
  353. paragraphs without a line break.  A disadvantage of using 'nowrap' is that you
  354. can't see the whole sentence you are working on.  When 'wrap' is on, words are
  355. broken halfway, which makes them hard to read.
  356.    A good solution for editing this kind of paragraph is setting the
  357. 'linebreak' option.  Vim then breaks lines at an appropriate place when
  358. displaying the line.  The text in the file remains unchanged.
  359.    Without 'linebreak' text might look like this:
  360.  
  361.     +---------------------------------+
  362.     |letter generation program for a b|
  363.     |ank.  They wanted to send out a s|
  364.     |pecial, personalized letter to th|
  365.     |eir richest 1000 customers.  Unfo|
  366.     |rtunately for the programmer, he |
  367.     +---------------------------------+
  368. After: >
  369.  
  370.     :set linebreak
  371.  
  372. it looks like this:
  373.  
  374.     +---------------------------------+
  375.     |letter generation program for a  |
  376.     |bank.  They wanted to send out a |
  377.     |special, personalized letter to  |
  378.     |their richest 1000 customers.    |
  379.     |Unfortunately for the programmer,|
  380.     +---------------------------------+
  381.  
  382. Related options:
  383. 'breakat' specifies the characters where a break can be inserted.
  384. 'showbreak' specifies a string to show at the start of broken line.
  385. Set 'textwidth' to zero to avoid a paragraph to be split.
  386.  
  387.  
  388. MOVING BY VISIBLE LINES
  389.  
  390. The "j" and "k" commands move to the next and previous lines.  When used on
  391. a long line, this means moving a lot of screen lines at once.
  392.    To move only one screen line, use the "gj" and "gk" commands.  When a line
  393. doesn't wrap they do the same as "j" and "k".  When the line does wrap, they
  394. move to a character displayed one line below or above.
  395.    You might like to use these mappings, which bind these movement commands to
  396. the cursor keys: >
  397.  
  398.     :map <Up> gk
  399.     :map <Down> gj
  400.  
  401. ==============================================================================
  402. *25.5*    Editing tables
  403.  
  404. Suppose you are editing a table with four columns:
  405.  
  406.     nice table      test 1    test 2        test 3 ~
  407.     input A          0.534 ~
  408.     input B          0.913 ~
  409.  
  410. You need to enter numbers in the third column.  You could move to the second
  411. line, use "A", enter a lot of spaces and type the text.
  412.    For this kind of editing there is a special option: >
  413.  
  414.     set virtualedit=all
  415.  
  416. Now you can move the cursor to positions where there isn't any text.  This is
  417. called "virtual space".  Editing a table is a lot easier this way.
  418.    Move the cursor by searching for the header of the last column: >
  419.  
  420.     /test 3
  421.  
  422. Now press "j" and you are right where you can enter the value for "input A".
  423. Typing "0.693" results in:
  424.  
  425.     nice table      test 1     test 2     test 3 ~
  426.     input A          0.534             0.693 ~
  427.     input B          0.913 ~
  428.  
  429. Vim has automatically filled the gap in front of the new text for you.  Now,
  430. to enter the next field in this column use "Bj".  "B" moves back to the start
  431. of a white space separated word.  Then "j" moves to the place where the next
  432. field can be entered.
  433.  
  434.     Note:
  435.     You can move the cursor anywhere in the display, also beyond the end
  436.     of a line.  But Vim will not insert spaces there, until you insert a
  437.     character in that position.
  438.  
  439.  
  440. COPYING A COLUMN
  441.  
  442. You want to add a column, which should be a copy of the third column and
  443. placed before the "test 1" column.  Do this in seven steps:
  444. 1.  Move the cursor to the left upper corner of this column, e.g., with
  445.     "/test 3".
  446. 2.  Press CTRL-V to start blockwise Visual mode.
  447. 3.  Move the cursor down two lines with "2j".  You are now in "virtual space":
  448.     the "input B" line of the "test 3" column.
  449. 4.  Move the cursor right, to include the whole column in the selection, plus
  450.     the space that you want between the columns.  "9l" should do it.
  451. 5.  Yank the selected rectangle with "y".
  452. 6.  Move the cursor to "test 1", where the new column must be placed.
  453. 7.  Press "P".
  454.  
  455. The result should be:
  456.  
  457.     nice table      test 3    test 1     test 2       test 3 ~
  458.     input A          0.693     0.534           0.693 ~
  459.     input B                0.913 ~
  460.  
  461. Notice that the whole "test 1" column was shifted right, also the line where
  462. the "test 3" column didn't have text.
  463.  
  464. Go back to non-virtual cursor movements with: >
  465.  
  466.     :set virtualedit=
  467.  
  468.  
  469. VIRTUAL REPLACE MODE
  470.  
  471. The disadvantage of using 'virtualedit' is that it "feels" different.  You
  472. can't recognize tabs or spaces beyond the end of line when moving the cursor
  473. around.  Another method can be used: Virtual replace mode.
  474.    Suppose you have a line in a table that contains both tabs and other
  475. characters.  Use "rx" on the first tab:
  476.  
  477.     inp    0.693   0.534    0.693 ~
  478.  
  479.            |
  480.        rx  |
  481.            V
  482.  
  483.     inpx0.693   0.534    0.693 ~
  484.  
  485. The layout is messed up.  To avoid that, use the "gr" command:
  486.  
  487.     inp    0.693   0.534    0.693 ~
  488.  
  489.            |
  490.       grx  |
  491.            V
  492.  
  493.     inpx    0.693   0.534    0.693 ~
  494.  
  495. What happens is that the "gr" command makes sure the new character takes the
  496. right amount of screen space.  Extra spaces or tabs are inserted to fill the
  497. gap.  Thus what actually happens is that a tab is replaced by "x" and then
  498. blanks added to make the text after it keep it's place.  In this case a
  499. tab is inserted.
  500.    When you need to replace more than one character, you use the "R" command
  501. to go to replace mode (see |04.9|).  This messes up the layout and replaces
  502. the wrong characters:
  503.  
  504.     inp    0    0.534    0.693 ~
  505.  
  506.         |
  507.      R0.786 |
  508.         V
  509.  
  510.     inp    0.78634    0.693 ~
  511.  
  512. The "gR" command uses virtual replace mode.  This preserves the layout:
  513.  
  514.     inp    0    0.534    0.693 ~
  515.  
  516.         |
  517.     gR0.786 |
  518.         V
  519.  
  520.     inp    0.786    0.534    0.693 ~
  521.  
  522. ==============================================================================
  523.  
  524. Next chapter: |usr_26.txt|  Repeating
  525.  
  526. Copyright: see |manual-copyright|  vim:tw=78:ts=8:ft=help:norl:
  527.